package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.service.LoginCustomerService;
import com.chixing.service.RegisterService;
import com.chixing.util.RSA.RSAUtil;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class LoginCustomerController {
    @Autowired
    private LoginCustomerService loginCustomerService;
    @Autowired
    private RegisterService registerService;

    // 跳转到注册页面
    @GetMapping("customer/register")
    public ModelAndView returnRegister(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("customer/register");
        return modelAndView;
    }

    // 跳转到登录页面
    @GetMapping("customer/login")
    public ModelAndView returnLogin(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("customer/login");
        return modelAndView;
    }
    // 登录
    @PostMapping("login")
    @ResponseBody
    public ServerResponse login(Long custTel, String custPassword, HttpSession session){
//        System.out.println("custTel:" + custTel);
//        System.out.println("custPassword:" + custPassword);
        ServerResponse serverResponse = registerService.Login1(custTel,custPassword);
        if(serverResponse.getCode()==200){
            session.setAttribute("loginCustomer",serverResponse.getData());
        }
        return serverResponse;
    }

    @GetMapping("customer/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginCustomer");
        return "redirect:/";
    }

}
